package com.blrs.controller;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.blrs.VO.AnswerVO;
import com.blrs.domain.HighSeas;
import com.blrs.domain.QuestionnaireAnswers;
import com.blrs.service.questionnaire.IQuestionnaireTitleService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import com.blrs.common.annotation.Log;
import com.blrs.common.core.controller.BaseController;
import com.blrs.common.core.domain.AjaxResult;
import com.blrs.common.enums.BusinessType;
import com.blrs.domain.QuestionnaireTitle;
import com.blrs.common.utils.poi.ExcelUtil;
import com.blrs.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 问卷题目Controller
 * 
 * @author yang
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/questionnaire/title")
public class QuestionnaireTitleController extends BaseController {
    @Autowired
    private IQuestionnaireTitleService questionnaireTitleService;

    /**
     * 查询问卷题目列表
     */
//    @PreAuthorize("@ss.hasPermi('questionnaire:title:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionnaireTitle questionnaireTitle) {
        startPage();
        List<QuestionnaireTitle> list = questionnaireTitleService.selectQuestionnaireTitleList(questionnaireTitle);
        return getDataTable(list);
    }

    /**
     * 导出问卷题目列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionnaireTitle questionnaireTitle) {
        List<QuestionnaireTitle> list = questionnaireTitleService.selectQuestionnaireTitleList(questionnaireTitle);
        for (QuestionnaireTitle title : list) {
            List<QuestionnaireAnswers> questionnaireAnswersList = title.getQuestionnaireAnswersList();
            for (int i = 0; i < questionnaireAnswersList.size() && i < 10; i++) {
                QuestionnaireAnswers answer = questionnaireAnswersList.get(i);
                switch (i) {
                    case 0:
                        title.setAnswerOne(answer.getSolutionName());
                        title.setAnswerOneNum(String.valueOf(answer.getScore()));
                        break;
                    case 1:
                        title.setAnswerTwo(answer.getSolutionName());
                        title.setAnswerTwoNum(String.valueOf(answer.getScore()));
                        break;
                    case 2:
                        title.setAnswerThree(answer.getSolutionName());
                        title.setAnswerThreeNum(String.valueOf(answer.getScore()));
                        break;
                    case 3:
                        title.setAnswerFour(answer.getSolutionName());
                        title.setAnswerFourNum(String.valueOf(answer.getScore()));
                        break;
                    case 4:
                        title.setAnswerFive(answer.getSolutionName());
                        title.setAnswerFiveNum(String.valueOf(answer.getScore()));
                        break;
                    case 5:
                        title.setAnswerSix(answer.getSolutionName());
                        title.setAnswerSixNum(String.valueOf(answer.getScore()));
                        break;
                    case 6:
                        title.setAnswerSeven(answer.getSolutionName());
                        title.setAnswerSevenNum(String.valueOf(answer.getScore()));
                        break;
                    case 7:
                        title.setAnswerEight(answer.getSolutionName());
                        title.setAnswerEightNum(String.valueOf(answer.getScore()));
                        break;
                    case 8:
                        title.setAnswerNine(answer.getSolutionName());
                        title.setAnswerNineNum(String.valueOf(answer.getScore()));
                        break;
                    case 9:
                        title.setAnswerTen(answer.getSolutionName());
                        title.setAnswerTenNum(String.valueOf(answer.getScore()));
                        break;
                }
            }
        }
        ExcelUtil<QuestionnaireTitle> util = new ExcelUtil<QuestionnaireTitle>(QuestionnaireTitle.class);
        util.exportExcel(response, list, "问卷题目数据");
    }


    /**
     * 获取问卷题目详细信息
     */
    @GetMapping(value = "/{topicId}")
    public AjaxResult getInfo(@PathVariable("topicId") Long topicId) {
        return success(questionnaireTitleService.selectQuestionnaireTitleByTopicId(topicId));
    }

    /**
     * 新增问卷题目
     */
//    @Log(title = "问卷题目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionnaireTitle questionnaireTitle) {
        return toAjax(questionnaireTitleService.insertQuestionnaireTitle(questionnaireTitle));
    }

    /**
     * 修改问卷题目
     */
    @Log(title = "问卷题目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionnaireTitle questionnaireTitle) {
        return questionnaireTitleService.updateQuestionnaireTitle(questionnaireTitle);
    }

    /**
     * 删除问卷题目
     */
    @Log(title = "问卷题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topicIds}")
    public AjaxResult remove(@PathVariable Long[] topicIds) {
        return toAjax(questionnaireTitleService.deleteQuestionnaireTitleByTopicIds(topicIds));
    }

    /**
     * 删除问卷题目
     */
    @Log(title = "问卷题目", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{topicId}")
    public AjaxResult removeByTopicId(@PathVariable Long topicId) {
        return toAjax(questionnaireTitleService.deleteQuestionnaireTitleByTopicId(topicId));
    }

    /**
     * 题目导入
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, @RequestParam String id) throws Exception {
        ExcelUtil<QuestionnaireTitle> util = new ExcelUtil(QuestionnaireTitle.class);
        List<QuestionnaireTitle> list = util.importExcel(file.getInputStream());
        return questionnaireTitleService.importExcel(list, id);
    }

    /**
     * 导出题目管理模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<QuestionnaireTitle> util = new ExcelUtil<>(QuestionnaireTitle.class);
        util.importTemplateExcel(response, "题目导出模板");
    }

    /**
     * 新增题目到指定的问卷中
     */
    @PostMapping("/saveByQuestionnaireId/{ids}/{questionnaireId}")
    public AjaxResult saveByQuestionnaireId(@PathVariable(name = "ids") Long[] ids, @PathVariable(name = "questionnaireId") String questionnaireId) {
        return questionnaireTitleService.saveTitleByQuestionnaireId(ids, questionnaireId);
    }

    /**
     * 查看问卷题目以及答案
     */
    @GetMapping("/selectQuestionnaireTitle/{questionnaireId}")
    public AjaxResult selectQuestionnaireTitle(@PathVariable(name = "questionnaireId") String questionnaireId) throws JsonProcessingException {
        return questionnaireTitleService.selectSelectQuestionnaireTitleById(questionnaireId);
    }

    /**
     * 发送方查看问卷
     */
    @GetMapping("/selectQuestionnaireTitleBySend/{questionnaireId}/{OrgId}")
    public AjaxResult selectQuestionnaireTitleBySend(@PathVariable(name = "questionnaireId") String questionnaireId,@PathVariable(name = "OrgId")Long orgId) throws JsonProcessingException {
        return questionnaireTitleService.selectSelectQuestionnaireTitleByIdOnSend(questionnaireId,orgId);
    }


}
